package com.atguigu.mall.mallproduct.controller;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.mall.mallproduct.entity.PmsCategoryEntity;
import com.atguigu.mall.mallproduct.service.PmsCategoryService;
import com.atguigu.common.utils.R;

/**
 * 商品三级分类
 *
 * @author Fuc
 * @email leftbear@gmail.com
 * @date 2020-10-21 14:01:49
 */
@RestController
@RequestMapping("product/category")
public class PmsCategoryController {
  @Autowired private PmsCategoryService pmsCategoryService;

  /** 查询所有分类以及子分类，以树形结构组装起来 */
  @RequestMapping("/list/tree")
  public R list() {
    List<PmsCategoryEntity> list = pmsCategoryService.listWithTree();
    return R.ok().put("data", list);
  }

  /** 信息 */
  @RequestMapping("/info/{catId}")
  public R info(@PathVariable("catId") Long catId) {
    PmsCategoryEntity pmsCategory = pmsCategoryService.getById(catId);

    return R.ok().put("pmsCategory", pmsCategory);
  }

  /** 保存 */
  @RequestMapping("/save")
  public R save(@RequestBody PmsCategoryEntity pmsCategory) {
    pmsCategoryService.save(pmsCategory);

    return R.ok();
  }

  /** 修改 */
  @RequestMapping("/update")
  public R update(@RequestBody PmsCategoryEntity pmsCategory) {
    pmsCategoryService.updateById(pmsCategory);

    return R.ok();
  }

  /**
   * 删除
   * @RequestBody:获取请求体，必须发送POST请求
   * SpringMVC自动将请求体的数据（JSON）,转换对应的对象
   */
  @RequestMapping("/delete")
  public R delete(@RequestBody Long[] catIds) {
    // 1.检查当前删除的菜单，是否被别的地方引用
    //    pmsCategoryService.removeByIds(Arrays.asList(catIds));
    pmsCategoryService.removeMenuByIds(Arrays.asList(catIds));
    return R.ok();
  }
}
